<template>
  <div class="new">
    <van-overlay :show="overlayShow" :z-index="1000">
      <div style="display: flex;align-items: center;justify-content: center;height: 100%;">
        <van-loading type="spinner" color="#1989fa">请稍候...</van-loading>
      </div>
    </van-overlay>
    <div style="margin-bottom: 10vh;">
      <van-collapse v-model="activeNames">
        <van-collapse-item v-for="item in jsondata.head" :key="item.grouptitle" :name="item.grouptitle" :title="item.grouptitle">
          <van-cell-group style="border:0px;">
            <div style="border-bottom:1px solid #ebedf0" v-for="itemdata in item.groupvalue" :key="itemdata.ID">
              <idatetime ishead :itemdata="itemdata" v-if="itemdata.sUIType == 'datetime'" :bzjsrqFieldVal="bzjsrqField" :xzksrqFieldVal="xzksrqField" :xzjsrqFieldVal="xzjsrqField" />
              <icode ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'code'" />
              <ienum ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'enum'" :jsondata="jsondata" :idobject="idobject" />
              <iref ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'ref'" :clickmodel.sync="clickmodel" :clickfrom.sync="clickfrom" :clickindexName.sync="clickindexName" :clickindex.sync="clickindex" :jsondata="jsondata" />
              <ibool ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'bool'" :jsondata="jsondata" :idobject="idobject" />
              <ifile ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'file'" :jsondata="jsondata" />
              <ipicture ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'picture'" :jsondata="jsondata" />
              <istring ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'string'" :jsondata="jsondata" :idobject="idobject" />
              <imap ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'map'" :jsondata="jsondata" :item="item" />
              <istring ishead :itemdata="itemdata" v-else :jsondata="jsondata" :idobject="idobject" />
            </div>
          </van-cell-group>
        </van-collapse-item>
      </van-collapse>
      <div v-for="(itembody, indexName) in jsondata.body" :key="indexName" :name="indexName" :title="indexName">
        <van-collapse v-model="activeBodyNams">
          <van-collapse-item v-for="(item, index) in jsondata.bodydata[indexName]" :key="indexName + index" :name="indexName + index" :title="indexName + (index + 1)">
            <van-cell-group style="border:0px;">
              <div style="border-bottom:1px solid #ebedf0" v-for="itemdata in item" :key="itemdata.ID">
                <idatetime :index="index" :itemdata="itemdata" v-if="itemdata.sUIType == 'datetime'" />
                <icode :index="index" :itemdata="itemdata" v-else-if="itemdata.sUIType == 'code'" />
                <ienum :index="index" :itemdata="itemdata" v-else-if="itemdata.sUIType == 'enum'" :jsondata="jsondata" :idobject="idobject" />
                <iref :index="index" :itemdata="itemdata" v-else-if="itemdata.sUIType == 'ref'" :clickmodel.sync="clickmodel" :clickfrom.sync="clickfrom" :clickindexName.sync="clickindexName" :clickindex.sync="clickindex" :jsondata="jsondata" :indexName="indexName" />
                <ibool :index="index" :itemdata="itemdata" v-else-if="itemdata.sUIType == 'bool'" :jsondata="jsondata" :idobject="idobject" />
                <ifile :index="index" :itemdata="itemdata" v-else-if="itemdata.sUIType == 'file'" :jsondata="jsondata" />
                <ipicture :index="index" :itemdata="itemdata" v-else-if="itemdata.sUIType == 'picture'" :jsondata="jsondata" />
                <istring :index="index" :itemdata="itemdata" v-else-if="itemdata.sUIType == 'string'" :jsondata="jsondata" :indexName="indexName" :idobject="idobject" />
                <imap :index="index" :itemdata="itemdata" v-else-if="itemdata.sUIType == 'map'" :jsondata="jsondata" :item="item" />
                <istring :index="index" :itemdata="itemdata" v-else :jsondata="jsondata" :indexName="indexName" :idobject="idobject" />
              </div>
              <van-button type="danger" size="mini" plain round style="float:right;" @click="delRow(indexName, index)">删除</van-button>
            </van-cell-group>
          </van-collapse-item>
        </van-collapse>
        <van-button v-show="jsondata.hasdetail === '1'" type="info" round style="margin-top: 2vh;margin-left: 2vw;" @click="addRow(indexName)">增行</van-button>
      </div>
      <div v-if="notemessage !== ''" style="margin:15px;color:red">{{ notemessage }}</div>
    </div>
    <van-tabbar v-model="active" safe-area-inset-bottom>
      <van-tabbar-item v-if="this.beforeSave && !this.afterSaveSucceed" @click="save">保存</van-tabbar-item>
      <van-tabbar-item v-if="isflow && this.beforeSave && !this.afterSaveSucceed" @click="savesubmit">保存并提交</van-tabbar-item>
      <van-tabbar-item v-if="isflow && !this.beforeSave && this.afterSaveSucceed" @click="submit">提交</van-tabbar-item>
      <van-tabbar-item v-if="isflow && !this.beforeSave && this.afterSaveSucceed" @click="audit">审核</van-tabbar-item>
      <!--      <van-tabbar-item v-if="this.idobject == 'arvouch'" @click="saveAudit">保存并审核</van-tabbar-item>-->
    </van-tabbar>
  </div>
</template>

<script>
import Vue from 'vue'
import { Search, List, Cell, PullRefresh, Toast, Tabbar, TabbarItem, Popup, DatetimePicker, ActionSheet, Tab, Tabs, Collapse, CollapseItem, CheckboxGroup, Checkbox, Uploader, Overlay, Loading, Rate } from 'vant'
import qs from 'qs'
import useInput from '@/utils/useinput'
Vue.use(useInput).use(Search).use(List).use(Cell).use(PullRefresh).use(Toast).use(Tabbar).use(TabbarItem).use(Popup).use(DatetimePicker).use(ActionSheet).use(Tab).use(Tabs).use(Collapse).use(CollapseItem).use(CheckboxGroup).use(Checkbox).use(Uploader).use(Overlay).use(Loading).use(Rate)
export default {
  name: 'new',
  data () {
    let _this = this
    return {
      idobject: this.$route.query.idobject,
      idtemplate: this.$route.query.idtemplate,
      func: this.$route.query.func,
      jsondata: {},
      index: 0,
      active: 0,
      activeNames: [''],
      activeBodyNams: [''],
      isLoad: false,
      conditionValHidden: '',
      clickmodel: {},
      clickfrom: '',
      clickindexName: '',
      clickindex: 0,
      isDisable: false,
      overlayShow: false,
      notemessage: '',
      isflow: false,
      bzjsrqField: '',
      xzksrqField: '',
      xzjsrqField: '',
      solrLoading: true,
      solrFinished: false,
      solrList: [],
      solrListDivStyle: {
        display: 'none'
      },
      events: {
        init: (o) => {
          //console.log(o.getCenter())
          _this.mapInstance = o
          let that = this
          //console.log('this:  ' + that)
          if (_this.isPosi === 0) {
            AMap.plugin('AMap.Geolocation', function () {
              var geolocation = new AMap.Geolocation({
                enableHighAccuracy: true,
                timeout: 10000,
                zoomEnable: false,
                dragEnable: false,
                showButton: false,
                buttonOffset: new AMap.Pixel(10, 20),
                zoomToAccuracy: true
              })
              o.addControl(geolocation)
              geolocation.getCurrentPosition(function (status, result) {
                if (status === 'complete') {
                  o.center = result.position
                  //console.log(result.position + '-------')
                  //console.log(result.formattedAddress)
                  that.place = result.formattedAddress
                  for (let groupindex in _this.jsondata.head) {
                    let itemgroup = _this.jsondata.head[groupindex].groupvalue
                    for (let itemindex in itemgroup) {
                      let itemdata = itemgroup[itemindex]
                      if (itemdata.sUIType === 'map') {
                        itemdata.value = result.formattedAddress + '&' + result.position
                      }
                    }
                  }
                } else {
                  alert(JSON.stringify(result))
                  //console.log(result)
                }
              })
            })
          }
        }
      },
      // 以下两个状态判断是否保存, 来决定显示保存按钮 后者是 保存并审核 保存并提交 按钮
      // 未保存
      beforeSave: true,
      // 保存成功
      afterSaveSucceed: false
    }
  },
  mounted: function () {
    //console.log('start created')
    //console.log('idobject', this.idobject)
    if (!this.isLoad) {
      let data = {
        [this.idobject + '_id']: this.$route.query.itemid,
        [this.idobject + '_idtemplate']: this.$route.query.idtemplate,
        'idobject': this.idobject
      }
      let url = '/api/' + this.idobject + '.' + this.func + '.do'

      this.axios({
        method: 'post',
        url: url,
        data: qs.stringify(data)
      }).then(res => {
        //console.log('renew data', res)
        if (res.data.flag === '1') {
          Toast(res.data.des)
          if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
            this.loginout()
          }
        } else {
          this.notemessage = res.data.notemessage
          this.isflow = res.data.isflow
          this.jsondata = res.data.jsondata
          for (let i = 0; i < res.data.jsondata.head.length; i++) {
            let head = res.data.jsondata.head[i]
            this.activeNames.push(head.grouptitle)
            if (this.idobject === 'weekreports') {
              for (let j = 0; j < head.groupvalue.length; j++) {
                let rec = head.groupvalue[j]
                if (rec.sField === 'bzjsrq') {
                  this.bzjsrqField = rec.value
                }
                if (rec.sField === 'xzksrq') {
                  this.xzksrqField = rec.value
                }
                if (rec.sField === 'xzjsrq') {
                  this.xzjsrqField = rec.value
                }
              }
            }
          }

          //console.log(this.jsondata)
          this.isLoad = true
        }
      }).catch(res => {
        this.reserror(res)
      })
    }
  },
  methods: {
    save () {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1

        // data[this.idobject + '_id'] = this.$route.query.itemid
        data[this.idobject + '_idtemplate'] = this.idtemplate
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                alert(itemdata.sTitle + '不能为空')
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        if (this.jsondata.hasdetail === '1') {
          for (let indexName in this.jsondata.bodydata) {
            for (let i = 0; i < this.jsondata.bodydata[indexName].length; i++) {
              let item = this.jsondata.bodydata[indexName][i]
              for (let itemindex in item) {
                let itemdata = item[itemindex]
                if (itemdata.sUIType === 'ref') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
                } else if (itemdata.sUIType === 'enum') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
                } else {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
                }
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.changesave.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            Toast('保存成功')
            if (this.$route.query.newflag === 'refnew') {
              this.$store.commit('refReBack', { ref_show: 'true' })
              this.$router.go(-1)
              return
            }
            this.$router.push({ name: 'list', query: { idobject: this.idobject } })
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log('err', res)
        })
      }
    },
    submit () {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        data[this.idobject + '_idtemplate'] = this.idtemplate
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        if (this.jsondata.hasdetail === '1') {
          for (let indexName in this.jsondata.bodydata) {
            for (let i = 0; i < this.jsondata.bodydata[indexName].length; i++) {
              let item = this.jsondata.bodydata[indexName][i]
              for (let itemindex in item) {
                let itemdata = item[itemindex]
                if (itemdata.sUIType === 'ref') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
                } else if (itemdata.sUIType === 'enum') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
                } else {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
                }
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.flowsubmit.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            this.beforeSave = false
            this.afterSaveSucceed = true
            Toast('提交成功')
            if (this.$route.query.newflag === 'refnew') {
              this.$store.commit('refReBack', { ref_show: 'true' })
              this.$router.go(-1)
              return
            }
            this.$router.push({ name: 'list', query: { idobject: this.idobject } })
            // 发起提交请求结束 --
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log(res)
        })
      }
    },
    savesubmit () {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        data[this.idobject + '_idtemplate'] = this.idtemplate
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                alert(itemdata.sTitle + '不能为空')
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        if (this.jsondata.hasdetail === '1') {
          for (let indexName in this.jsondata.bodydata) {
            for (let i = 0; i < this.jsondata.bodydata[indexName].length; i++) {
              let item = this.jsondata.bodydata[indexName][i]
              for (let itemindex in item) {
                let itemdata = item[itemindex]
                if (itemdata.sUIType === 'ref') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
                } else if (itemdata.sUIType === 'enum') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
                } else {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
                }
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.newsave.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            this.isDisable = true
            this.overlayShow = true
            this.beforeSave = false
            this.afterSaveSucceed = true
            Toast('保存成功...开始提交...')
            // 发起提交请求开始 --
            this.axios({
              method: 'post',
              url: '/api/' + this.idobject + '.flowsubmit.do',
              data: qs.stringify(data)
            }).then(res => {
              this.isDisable = false
              this.overlayShow = false
              if (res.data.flag === '0') {
                Toast('提交成功')
                if (this.$route.query.newflag === 'refnew') {
                  this.$store.commit('refReBack', { ref_show: 'true' })
                  this.$router.go(-1)
                  return
                }
                this.$router.push({ name: 'list', query: { idobject: this.idobject } })
              } else {
                Toast(res.data.des)
                if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
                  this.loginout()
                }
              }
              //console.log(res)
            }).catch(res => {
              this.isDisable = false
              this.overlayShow = false
              //console.log(res)
            })

            // 发起提交请求结束 --
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log(res)
        })
      }
    },
    saveAudit () {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        if (this.jsondata.hasdetail === '1') {
          for (let i = 0; i < this.jsondata.bodydata.length; i++) {
            let item = this.jsondata.bodydata[i]
            for (let itemindex in item) {
              let itemdata = item[itemindex]
              if (itemdata.sUIType === 'ref') {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
              } else if (itemdata.sUIType === 'enum') {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
              } else {
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
                data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.newsaveaudit.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            Toast('保存并审核成功')
            this.$router.push({ name: 'list', query: { idobject: this.idobject } })
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log(res)
        })
      }
    },
    addRow (indexName) {
      let bodycopy = JSON.parse(JSON.stringify(this.jsondata.body[indexName]))
      this.jsondata.bodydata[indexName].push(bodycopy)
    },
    delRow (indexName, index) {
      if (this.jsondata.bodydata[indexName].length === 1) {
        Toast('至少保留一条表体行，不能删除！')
        return
      }
      this.jsondata.bodydata[indexName].splice(index, 1)
    },
    toggle (index) {
      this.$refs.checkboxes[index].toggle()
    }
  },
  activated () {
    let a = { aa: 'aaa' }
    //console.log('a1', a)
    //console.log('clickmodel1', this.clickmodel)
    //console.log('clickindex1', this.clickindex)
    //console.log('clickindexName1', this.clickindexName)
    //console.log('clickfrom1', this.clickfrom)
    if (this.$store.state.ref_show !== '') {
      this.clickmodel.value = this.$store.state.ref_show
      this.clickmodel.valuehidden = this.$store.state.ref_id
      this.clickmodel.errormsg = ''
      let fieldmap = this.clickmodel.fieldmap
      //console.log('fieldmap', fieldmap)
      if (fieldmap !== undefined && fieldmap.length > 0) {
        let arrFieldmap = fieldmap.split(';')
        let item = this.$store.state.ref_item
        if (this.clickfrom === 'headdata') {
          a.aa = 'bbb'
          for (let groupindex = 0; groupindex < this.jsondata.head.length; groupindex++) {
            let itemgroup = this.jsondata.head[groupindex]
            for (let itemindex = 0; itemindex < itemgroup.groupvalue.length; itemindex++) {
              let itemdata = itemgroup.groupvalue[itemindex]
              for (let fieldmapindex in arrFieldmap) {
                let arrFieldMapKey = arrFieldmap[fieldmapindex].split(':')
                if (this.$store.state.ref_field === arrFieldMapKey[1]) {
                  continue
                }
                if (arrFieldMapKey.length === 3 && arrFieldMapKey[2] !== '' && this.$store.state.ref_field !== arrFieldMapKey[2]) {
                  continue
                }
                if (itemdata.sField === arrFieldMapKey[1]) {
                  if (itemdata.sUIType === 'ref') {
                    itemdata.value = item[arrFieldMapKey[0] + '_ref']
                    itemdata.valuehidden = item[arrFieldMapKey[0]]
                  } else if (itemdata.sUIType === 'enum') {
                    itemdata.value = item[arrFieldMapKey[0] + '_enum']
                    itemdata.valuehidden = item[arrFieldMapKey[0]]
                  } else {
                    itemdata.value = item[arrFieldMapKey[0]]
                  }
                }
              }
            }
          }
        } else if (this.clickfrom === 'bodydata') {
          a.aa = 'ccc'
          for (let itemindex in this.jsondata.bodydata[this.clickindexName][this.clickindex]) {
            let itemdata = this.jsondata.bodydata[this.clickindexName][this.clickindex][itemindex]
            for (let fieldmapindex in arrFieldmap) {
              let arrFieldMapKey = arrFieldmap[fieldmapindex].split(':')
              if (this.$store.state.ref_field === arrFieldMapKey[1]) {
                continue
              }
              if (arrFieldMapKey.length === 3 && arrFieldMapKey[2] !== '' && this.$store.state.ref_field !== arrFieldMapKey[2]) {
                continue
              }
              if (itemdata.sField === arrFieldMapKey[1]) {
                if (itemdata.sUIType === 'ref') {
                  itemdata.value = item[arrFieldMapKey[0] + '_ref']
                  itemdata.valuehidden = item[arrFieldMapKey[0]]
                } else if (itemdata.sUIType === 'enum') {
                  itemdata.value = item[arrFieldMapKey[0] + '_enum']
                  itemdata.valuehidden = item[arrFieldMapKey[0]]
                } else {
                  itemdata.value = item[arrFieldMapKey[0]]
                }
              }
            }
          }
        }
      }
      //console.log('a', a)
      //console.log('clickmodel', this.clickmodel)
      //console.log('clickindex', this.clickindex)
      //console.log('clickindexName', this.clickindexName)
      //console.log('clickfrom', this.clickfrom)
      this.clickmodel = {}
      this.clickindex = 0
      this.clickindexName = ''
      this.clickfrom = ''
      this.$store.commit('clearRef')
    }
  }
}
</script>

<style>
.fieldgroup_title {
  text-align: left;
  margin: 0px;
  padding: 25px 15px 15px;
  color: rgba(69, 90, 100, 0.6);
  font-weight: normal;
  font-size: 14px;
}
</style>
